﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Capokun;
using Capokun.Models;
using System.IO;
using System.Xml.Serialization;

namespace Capokun.Controllers
{
    public class MobileController : Controller
    {
        //string songFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Songs\\";

        //public void Update()
        //{
        //    try
        //    {
        //        List<Song> currentSongs = CapokunContext.GetAllSongs();
        //        currentSongs[0].Link = "new link";
        //        CapokunContext
        //    }
        //    catch (Exception)
        //    {
        //        throw;
        //    }
        //}


        //
        // GET: /Mobile/

        //public ActionResult Index()
        //{
        //    ///one time filling xml
        //    //this.oneTimeSerialize();




        //    string[] files = Directory.GetFiles(songFolder);

        //    List<string> filesList = new List<string>();

        //    for (int i = 0; i < files.Length; i++)
        //    {
        //        string songName = System.IO.Path.GetFileName(files[i]);
        //        songName = songName.Replace(".html", "");
        //        filesList.Add(songName);
        //    }
        //    return View(filesList);
        //}

        //private void oneTimeSerialize()
        //{
        //    List<Song> songToSerialize = new List<Models.Song>();
        //    string[] files = Directory.GetFiles(songFolder);
        //    for (int i = 0; i < files.Length; i++)
        //    {
        //        Song newSong = new Models.Song();
        //        newSong.SongId = System.IO.Path.GetFileName(files[i]);
        //        using (TextReader reader = System.IO.File.OpenText(files[i]))
        //        {
        //            string text = reader.ReadToEnd();
        //            text = filterText(text);
        //            newSong.Content = text;
        //        }
        //        songToSerialize.Add(newSong);
        //    }
        //    this.SerializeToXML(songToSerialize);
        //}

        public ActionResult Song(string id)
        {
            string songFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Songs\\";
            string fileName = songFolder + id + ".html";

            Song model = new Song();
            model.SongId = id;

            using (TextReader reader = System.IO.File.OpenText(fileName))
            {
                string text = reader.ReadToEnd();
                text = filterText(text);
                model.Content = text;
            }

            return View(model);
        }

        private string filterText(string text)
        {
            //text = text.Replace("&lt;", "<");
            //text = text.Replace("&gt;", "<");
            return text;
        }



        //public void SerializeToXML(List<Song> songs)
        //{
        //    XmlSerializer serializer = new XmlSerializer(typeof(List<Song>));
        //    string xmlpath = Server.MapPath(Url.Content("~/Content/CapoSongs.xml"));
        //    TextWriter textWriter = new StreamWriter(xmlpath);
        //    serializer.Serialize(textWriter, songs);
        //    textWriter.Close();
        //}


        //private List<Song> deserializeFromXML()
        //{
        //    string xmlpath = Server.MapPath(Url.Content("~/Content/CapoSongs.xml"));

        //    XmlSerializer deserializer = new XmlSerializer(typeof(List<Song>));
        //    TextReader textReader = new StreamReader(xmlpath);
        //    List<Song> songs = (List<Song>)deserializer.Deserialize(textReader);
        //    textReader.Close();
        //    songs = songs.OrderBy(x => x.SongId).ToList();
        //    return songs;
        //}

        /// <summary>
        /// ask all songs from server
        /// </summary>
        /// <returns></returns>
        //public JsonResult GoOffline()
        //{
        //    List<Song> allSongs = deserializeFromXML();
        //    return Json(allSongs.ToArray(), JsonRequestBehavior.AllowGet);
        //}

        /// <summary>
        /// get all songs titles
        /// </summary>
        /// <returns></returns>
        public JsonResult GetSongTitles()
        {
            List<string> songTitles = new List<string>();
            IEnumerable<Song> allSongs = CapokunContext.GetAllSongs().OrderBy(x => x.SongId);
            foreach (Song song in allSongs)
            {
                songTitles.Add(song.SongId);
            }
            return Json(songTitles.ToArray(), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSongContent(string songId)
        {
			IEnumerable<Song> allSongs = CapokunContext.GetAllSongs();
            foreach (Song song in allSongs)
            {
                if (songId == song.SongId)
                {
                    return Json(song, JsonRequestBehavior.AllowGet);
                }
            }
            return Json("", JsonRequestBehavior.AllowGet);
        }

    }
}
